﻿
using System.Collections.Generic;
using System.Data;
using System;
namespace ZHDJ.MobileInterface
{
    public partial class GetVoteActivityExList : AbstractBasePage
    {
        protected override OutParam BusinessProcess<InParam, OutParam>(InParam inParam)
        {
            // 业务逻辑返回的响应对象  
            GetVoteActivityExOutParam result = new GetVoteActivityExOutParam();
            GetVoteActivityExInParam param = new GetVoteActivityExInParam();
            string logMsg = "";
            bool isFlag = true;
                 
            try
            {
                param = inParam as GetVoteActivityExInParam;
                if (string.IsNullOrEmpty(param.user_id))
                {
                    result.code = "0";
                    result.desc = "用户ID不能为空";

                    isFlag = false;
                }
                if (string.IsNullOrEmpty(param.State))
                {
                    result.code = "0";
                    result.desc = "类型不能为空";

                    isFlag = false;
                }

                if (CommonUtils.IsDisableUsers(param.user_id))
                {
                    result.code = "0";
                    result.desc = "用户状态有问题，请联系管理员";

                    isFlag = false;
                }
                if (isFlag)
                {
                    //获取机关民主评议
                    //DataSet dsVote = InterfaceHelper.GetVoteActivityByID(ServiceSettings.VoteActivityID);
                    DataSet dsVote = InterfaceHelper.GetVoteActivityByID();
                    if (dsVote != null && dsVote.Tables[0].Rows.Count > 0)
                    {
                        VoteActivityEx voteTemp = null; 
                        foreach (DataRow dr in dsVote.Tables[0].Rows)
                        {
                            voteTemp = new VoteActivityEx();
                            voteTemp.ActivityID = dr["ActivityID"].ToString();
                            voteTemp.ActivityName = dr["ActivityName"].ToString();
                            voteTemp.ActivityDescribe = dr["ActivityDescribe"].ToString();
                            voteTemp.BeginTime = dr["BeginTime"].ToString();
                            voteTemp.EndTime = dr["EndTime"].ToString();
                            voteTemp.HaveVoteNum = "0";
                            voteTemp.NotVoteNum = "0";
                            voteTemp.VoteItemNum = "0";

                            try
                            {
                                string nowDate = DateTime.Now.ToString("yyyy-MM-dd");
                                if (Convert.ToDateTime(voteTemp.BeginTime) > Convert.ToDateTime(nowDate))
                                    continue;

                                if(Convert.ToDateTime(voteTemp.EndTime) < Convert.ToDateTime(nowDate))
                                    continue;
                            }
                            catch
                            {
                                continue;
                            }
                             
                            //获取被评议对象列表
                            DataSet ds = InterfaceHelper.GetVoteItemByID(param.user_id, voteTemp.ActivityID,param.State);
                            if (ds != null && ds.Tables[0].Rows.Count > 0)
                            {
                                int haveVoteNum = 0; 
                                foreach (DataRow drr in ds.Tables[0].Rows)
                                {  
                                    if (Convert.ToString(drr["VoteState"]) == "1")
                                        haveVoteNum++;
                                }

                                voteTemp.VoteItemNum = ds.Tables[0].Rows.Count.ToString();
                                voteTemp.HaveVoteNum = haveVoteNum.ToString();
                                voteTemp.NotVoteNum = (ds.Tables[0].Rows.Count - haveVoteNum).ToString();
                            }
                            result.data.list.Add(voteTemp);
                        } 
                    }
                    else
                    {
                        result.code = "0";
                        result.desc = "获取机关评议信息失败";

                        isFlag = false;
                    }
                }
                 
                logMsg = result.desc;
            }
            catch (Exception ex)
            {
                result.code = "0";
                result.desc = "操作异常，多次出现请联系管理员"; 
                logMsg = ex.Message + ":" + ex.StackTrace;
            }

            CommonUtils.InsertMobileInterfaceLog(SystemCode +"-GetGetVoteActivityExList", SerializeHelper.JsonSerializer<GetVoteActivityExInParam>(param), logMsg);  
            return result as OutParam;
        }

        protected void Page_Load(object sender, System.EventArgs e)
        {
            if (string.Compare(Request.QueryString["Safe"], "False", true) == 0)
                ProcessRequest<GetVoteActivityExInParam, GetVoteActivityExOutParam>();
            else
                SafeProcessRequest<GetVoteActivityExInParam, GetVoteActivityExOutParam>();
        }
    }

    #region 请求参数
    public class GetVoteActivityExInParam
    {
        /// <summary>
        /// 必填， 	用户ＩＤ
        /// </summary>
        public string user_id
        { get; set; }

        /// <summary>
        /// 分组（类型）
        /// </summary>
        public string State
        { get; set; }  
    }
    #endregion

    #region 结果参数
    public class GetVoteActivityExOutParam
    {
        public GetVoteActivityExOutParam()
        {
            code = "1";
            desc = "操作成功";
            data = new VoteActivityExList();
        }

        /// <summary>
        /// 返回结果 具体见接口返回对照码 1	成功 0	失败 2	请求非法 加密结果和传入的MD5字符串进行比较，如果相同则执行接口操作，否则返回1001错误响应码
        /// </summary>
        public string code
        { get; set; }

        /// <summary>
        /// 结果描述 成功和失败都需要
        /// </summary>
        public string desc
        { get; set; }

        /// <summary>
        /// 机关民主评议列表
        /// </summary>
        public VoteActivityExList data
        { get; set; }
    }

    public class VoteActivityExList
    {
        public VoteActivityExList()
        {
            list = new List<VoteActivityEx>();
        }

        public List<VoteActivityEx> list { get; set; }
    }

    public class VoteActivityEx
    {  
        /// <summary>
        ///	机关评议id
        /// </summary>
        public string ActivityID
        { get; set; }

        /// <summary>
        ///	机关评议标题
        /// </summary>
        public string ActivityName
        { get; set; }

        /// <summary>
        ///	评议描述说明
        /// </summary>
        public string ActivityDescribe
        { get; set; }

        /// <summary>
        ///	活动开始时间	
        /// </summary>
        public string BeginTime
        { get; set; }  

        /// <summary>
        ///	活动结束时间	
        /// </summary>
        public string EndTime
        { get; set; }  

        /// <summary>
        ///	被评议对象数量	
        /// </summary>
        public string VoteItemNum
        { get; set; }  

        /// <summary>
        ///	已评议数量
        /// </summary>
        public string HaveVoteNum
        { get; set; }  

        /// <summary>
        ///	未评议数量	
        /// </summary>
        public string NotVoteNum
        { get; set; } 
    } 
    #endregion 
}